﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class Main : Form
    {
        /***
         * 
         * 声明静态对象： 限制子窗体只能打开一个实例
         * 
         */
        //private static FrmNameManage frmNameManage = null;
        //private static FrmPathConfig frmPathConfig = null;
        //private static FrmSR frmSR = null;
        //private static FrmZC frmZC = null;
        //private static Frmday frmday = null;
        //private static FrmWeek frmWeek = null;
        //private static FrmMouth frmMouth = null;

        /*** 
         * 一 等同 二
         * 一、 定义一个命名方法
         *  private void OnNameManageFormClosed(object sender, FormClosedEventArgs e)
         *  {
         *     窗体关闭后，释放引用
         *      frmNameManage = null;  
         *  }
         *
         *   在某个初始化方法中注册事件
         *  private void InitializeForm()
         *  {
         *       注册事件处理方法
         *      frmNameManage.FormClosed += OnNameManageFormClosed;
         *  }
         *  
         * 二、 当 frmNameManage 窗体关闭时，自动将其引用设为 null，防止内存泄漏并允许垃圾回收器回收该对象。
         *  frmNameManage.FormClosed += (s, arg) => frmNameManage = null;
         */

        public Main()
        {
            InitializeComponent();
            this.IsMdiContainer = true; // 设置为主窗体
        }
        private void Main_Load(object sender, EventArgs e)
        {


            #region 读取文件获取配置参数 存在 则回填数据  否则忽略
            string defalutKey = "dataFilePath";
            string defalutValue = "";
            defalutValue = AppHelper.GetValue(defalutKey);
            if (string.IsNullOrEmpty(defalutValue))
            {
                defalutValue = ConfigurationManager.AppSettings[defalutKey].ToString();
            }

            if (!string.IsNullOrEmpty(defalutValue) && File.Exists(defalutValue))
            {

                CommonHelper.setFilePath = defalutValue;

                string[] setPathStr = CommonHelper.setFilePath.Split('/');
                CommonHelper.storePath = setPathStr[0];//存储文件夹路径

                List<string> lines = TextHelper.ReadTextAllLines(CommonHelper.setFilePath);
                //路径解析
                if (lines.Count > 0)
                {
                    CommonHelper.itemPath = lines[1].Split(' ')[1];//名目文件的路径
                    CommonHelper.incomePath = lines[2].Split(' ')[1];//收入信息文件的路径
                    CommonHelper.expendPath = lines[3].Split(' ')[1];//支出信息文件的路径
                    if (!string.IsNullOrEmpty(lines[4].Split(' ')[1]))
                    {
                        CommonHelper.dayPath = lines[4].Split(' ')[1];//日统计文件的路径
                    }
                    if (!string.IsNullOrEmpty(lines[5].Split(' ')[1]))
                    {
                        CommonHelper.weekPath = lines[5].Split(' ')[1];//周统计文件的路径
                    }
                    if (!string.IsNullOrEmpty(lines[6].Split(' ')[1]))
                    {
                        CommonHelper.monthPath = lines[6].Split(' ')[1];//月统计文件的路径
                    }
                }
            }
            #endregion

        }

        /// <summary>
        /// 名目维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNameManzge_Click(object sender, EventArgs e)
        {
            //if (frmNameManage == null || frmNameManage.IsDisposed)
            //{
            //    frmNameManage = new FrmNameManage();
            //    // 当 frmNameManage 窗体关闭时，自动将其引用设为 null，防止内存泄漏并允许垃圾回收器回收该对象。
            //    frmNameManage.FormClosed += (s, arg) => frmNameManage = null;
            //    frmNameManage.MdiParent = this;
            //    frmNameManage.Show();
            //}
            //else
            //{
            //    if (frmNameManage.WindowState == FormWindowState.Minimized)
            //    {
            //        frmNameManage.WindowState = FormWindowState.Normal;
            //    }
            //    frmNameManage.Activate();
            //}

            FormHelper.ShowForm<FrmNameManage>(this);
        }
        /// <summary>
        /// 文件路径配置维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPath_Click(object sender, EventArgs e)
        {
            //if (frmPathConfig == null || frmPathConfig.IsDisposed)
            //{
            //    frmPathConfig = new FrmPathConfig();
            //    frmPathConfig.FormClosed += (s, arg) => frmPathConfig = null;
            //    frmPathConfig.MdiParent = this;
            //    frmPathConfig.Show();
            //}
            //else
            //{
            //    if (frmPathConfig.WindowState == FormWindowState.Minimized)
            //    {
            //        frmPathConfig.WindowState = FormWindowState.Normal;
            //    }
            //    frmPathConfig.Activate();
            //}
            FormHelper.ShowForm<FrmPathConfig>(this);
        }
        /// <summary>
        /// 收入管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSR_Click(object sender, EventArgs e)
        {
            //if (frmSR == null || frmSR.IsDisposed)
            //{
            //    frmSR = new FrmSR();
            //    frmSR.FormClosed += (s, arg) => frmSR = null;
            //    frmSR.MdiParent = this;
            //    frmSR.Show();
            //}
            //else
            //{
            //    if (frmSR.WindowState == FormWindowState.Minimized)
            //    {
            //        frmSR.WindowState = FormWindowState.Normal;
            //    }
            //    frmSR.Activate();
            //}
            FormHelper.ShowForm<FrmSR>(this);
        }
        /// <summary>
        /// 支出管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnZC_Click(object sender, EventArgs e)
        {
            //if (frmZC == null || frmZC.IsDisposed)
            //{
            //    frmZC = new FrmZC();
            //    frmZC.FormClosed += (s, arg) => frmZC = null;
            //    frmZC.MdiParent = this;
            //    frmZC.Show();
            //}
            //else
            //{
            //    if (frmZC.WindowState == FormWindowState.Minimized)
            //    {
            //        frmZC.WindowState = FormWindowState.Normal;
            //    }
            //    frmZC.Activate();
            //}
            FormHelper.ShowForm<FrmZC>(this);
        }
        /// <summary>
        /// 日统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDay_Click(object sender, EventArgs e)
        {
            //if (frmday == null || frmday.IsDisposed)
            //{
            //    frmday = new Frmday();
            //    frmday.FormClosed += (s, arg) => frmday = null;
            //    frmday.MdiParent = this;
            //    frmday.Show();
            //}
            //else
            //{
            //    if (frmday.WindowState == FormWindowState.Minimized)
            //    {
            //        frmday.WindowState = FormWindowState.Normal;
            //    }
            //    frmday.Activate();
            //}
            FormHelper.ShowForm<Frmday>(this);
        }
        /// <summary>
        /// 周统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnweek_Click(object sender, EventArgs e)
        {
            //if (frmWeek == null || frmWeek.IsDisposed)
            //{
            //    frmWeek = new FrmWeek();
            //    frmWeek.FormClosed += (s, arg) => frmWeek = null;
            //    frmWeek.MdiParent = this;
            //    frmWeek.Show();
            //}
            //else
            //{
            //    if (frmWeek.WindowState == FormWindowState.Minimized)
            //    {
            //        frmWeek.WindowState = FormWindowState.Normal;
            //    }
            //    frmWeek.Activate();
            //}
            FormHelper.ShowForm<FrmWeek>(this);
        }
        /// <summary>
        /// 月统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMouth_Click(object sender, EventArgs e)
        {
            //if (frmMouth == null || frmMouth.IsDisposed)
            //{
            //    frmMouth = new FrmMouth();
            //    frmMouth.FormClosed += (s, arg) => frmMouth = null;
            //    frmMouth.MdiParent = this;
            //    frmMouth.StartPosition =  FormStartPosition.CenterScreen; 
            //    frmMouth.Show();
            //}
            //else
            //{
            //    if (frmMouth.WindowState == FormWindowState.Minimized)
            //    {
            //        frmMouth.WindowState = FormWindowState.Normal; 
            //    }
            //    frmMouth.Activate();
            //}
            FormHelper.ShowForm<FrmMouth>(this);

        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            //DialogResult dialogResult = MessageBox.Show("确定要关闭吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            //if (dialogResult == DialogResult.Yes)
            //{
            //    Application.Exit();
            //}
            Application.Exit();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogResult = MessageBox.Show("确定要关闭吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }
            //Application.ExitThread()：终止单个线程的消息循环，用于多线程 UI 场景。
            //Application.Exit()：优雅退出整个应用程序，触发所有窗体的关闭事件。
            //if (dialogResult == DialogResult.Yes)
            //{
            //    Application.ExitThread();
            //}else
            //    e.Cancel = true;
        }
    }

}
